interface dataSourceType {
  [propName: string]: any;
}

//过滤不可选组织，包含组织本身和自身下级组织
const getOptions = (id: number | string | any, upOrgOptions_f: dataSourceType[]) => {
  let OriginData: dataSourceType = JSON.parse(JSON.stringify(upOrgOptions_f));
  //不使用深拷贝会产生无限循环
  let options =
    OriginData instanceof Array
      ? JSON.parse(JSON.stringify(OriginData))
      : [JSON.parse(JSON.stringify(OriginData))];
  const loop = function (data: dataSourceType[], id: string | number) {
    if (data instanceof Array && data.length) {
      return data.map((d) => {
        d.disabled = d.id == id || d.parentId == id;
        if (d.children instanceof Array && d.children.length) {
          d.children = loop(d.children, d.parentId == id ? d.id : id);
        }
        return d;
      });
    }
  };
  return loop(options, id);
};
//过滤不可选组织，只能选子级
export const getOptions_childs = (id: number | string | any, upOrgOptions_f: dataSourceType[]) => {
  console.log(upOrgOptions_f, 23333223);

  let OriginData: dataSourceType = JSON.parse(JSON.stringify(upOrgOptions_f));
  //不使用深拷贝会产生无限循环
  let options =
    OriginData instanceof Array
      ? JSON.parse(JSON.stringify(OriginData))
      : [JSON.parse(JSON.stringify(OriginData))];
  const loop = function (data: dataSourceType[], id: string | number) {
    if (data instanceof Array && data.length) {
      return data.map((d) => {
        d.disabled = !(d.parentId == id) || d.id == id;
        if (d.children instanceof Array && d.children.length) {
          d.children = loop(d.children, d.parentId == id ? d.id : id);
        }
        return d;
      });
    }
  };
  return loop(options, id);
};

export default getOptions;
